﻿using System;
using Microsoft.AspNetCore.Http;
using FreeSql.DataAnnotations;
using EAF.DBC;
using EAF.Utility;

namespace App.Model
{
    /// <summary>
    /// 数据表映射实体通用字段 主键类型支持int和long
    /// </summary>
    /// <typeparam name="TPK">主键类型</typeparam>
    public class EntityBase<TPK> : PrimaryKeyColumn<TPK>
    {
        /// <summary>
        /// 初始化通用字段默认值 自动设置Valid、Deleted、CreateTime、创建人信息 因为查询数据时不需要这些操作所以改用普通方法初始化按需调用
        /// </summary>
        /// <param name="httpContext">用于获取登录信息</param>
        /// <returns></returns>
        public EntityBase<TPK> InitializeGeneralField(HttpContext httpContext = null)
        {
            Valid = true;
            Deleted = false;
            CreateTime = DateTime.Now;

            if (httpContext == null)
            {
                httpContext = ComHttpContext.Current;
            }
            CreaterId = httpContext.GetUserId();
            CreaterName = httpContext.GetUserName();
            return this;
        }

        /// <summary>
        /// 描述
        /// </summary>
        [Column(StringLength = 2000, IsNullable = true, Position = -10)]
        public virtual string Description { get; set; }
        /// <summary>
        /// 排序数字
        /// </summary>
        [Column(Position = -9)]
        public virtual int? SortNo { get; set; }
        /// <summary>
        /// 创建人ID
        /// </summary>
        [Column(Position = -8, CanUpdate = false)]
        public virtual int? CreaterId { get; set; }
        /// <summary>
        /// 创建人名
        /// </summary>
        [Column(Position = -7, IsNullable = true, StringLength = 50, CanUpdate = false)]
        public virtual string CreaterName { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        [Column(Position = -6, CanUpdate = false)]
        public virtual DateTime CreateTime { get; set; }
        /// <summary>
        /// 修改时间
        /// </summary>
        [Column(Position = -5, CanInsert = false)]
        public virtual DateTime? UpdateTime { get; set; }
        /// <summary>
        /// 是否有效
        /// </summary>
        [Column(Position = -4)]
        public virtual bool Valid { get; set; }
        /// <summary>
        /// 是否删除
        /// </summary>
        [Column(Position = -3)]
        public virtual bool Deleted { get; set; }
        /// <summary>
        /// 删除时间
        /// </summary>
        [Column(Position = -2)]
        public virtual DateTime? DeleteTime { get; set; }
        /// <summary>
        /// 数据版本 业务中可无视此字段
        /// </summary>
        [Column(Position = -1, IsVersion = true)]
        public virtual int Version { get; set; } = 0;

    }
}
